home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / win2maccountersamples / 4. counterdocument / source / ccounterdocument.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  8.0 KB  |  299 lines

  1. /*
  2.     File:        CCounterDocument.cp
  3.  
  4.     Contains:    Sample code to accompany Chapter 12 of 
  5.                 "An Introduction to Macintosh Programming for Windows Programmers".
  6.                 
  7.     Written by:    Worldwide Developer Technical Support
  8.  
  9.     Copyright:    1999 Apple Computer, Inc., All Rights Reserved
  10.  
  11.       You may incorporate this sample code into your applications without
  12.       restriction, though the sample code has been provided "AS IS" and the
  13.       responsibility for its operation is 100% yours.  However, what you are
  14.       not permitted to do is to redistribute the source as "DSC Sample Code"
  15.       after having made changes. If you're going to re-distribute the source,
  16.      we require that you make it clear in the source that the code was
  17.     descended from Apple Sample Code, but that you've made changes.
  18.     
  19. */
  20. #include <LFile.h>
  21. #include <LPlaceHolder.h>
  22. #include <LPrintout.h>
  23. #include <LString.h>
  24. #include <LWindow.h>
  25. #include <PP_Messages.h>
  26. #include <UMemoryMgr.h>
  27. #include <UWindows.h>
  28. #include <UModalDialogs.h>
  29.  
  30. #include <iostream>                // console for debugging
  31. using namespace std;              //introduces namespace std
  32.  
  33. #include "CounterConstants.h"
  34. #include "CCounterDocument.h"
  35.  
  36. //==================================================================================
  37. CCounterDocument::CCounterDocument(LCommander* inSuper, FSSpec*    inFileSpec)
  38.     : LSingleDoc(inSuper)
  39. {
  40.     mWindow = nil;
  41.     mWindow = MakeControlsWindow();
  42.     ThrowIfNil_(mWindow);
  43.     if (inFileSpec == nil) {
  44.         NameNewDoc();
  45.     } else {
  46.         OpenFile(*inFileSpec);
  47.     }
  48.     mWindow->Show();
  49.     mIsDirty = false;
  50. }
  51.  
  52. // ---------------------------------------------------------------------------
  53. //    Respond to commands from menus, buttons, etc.
  54. Boolean
  55. CCounterDocument::ObeyCommand(CommandT    inCommand, void* ioParam)
  56. {
  57.     Boolean    cmdHandled = true;
  58.     switch (inCommand) {
  59.         case cmd_SetValue:
  60.             Int32 newValue = 0;
  61.             if (AskForValue(newValue)) {
  62.                 stCounter.SetValue(newValue);
  63.                 mCaption->SetValue(stCounter.GetValue());
  64.                 mIsDirty = true;
  65.             }
  66.             break;
  67.  
  68.         case cmd_Increment:
  69. //            ::SysBeep(1);
  70.             stCounter.Increment();
  71.             mCaption->SetValue(stCounter.GetValue());
  72.             mIsDirty = true;
  73.             break;
  74.  
  75.         case cmd_Decrement:
  76.             stCounter.Decrement();
  77.             mCaption->SetValue(stCounter.GetValue());
  78.             mIsDirty = true;
  79.             break;
  80.  
  81.         default:
  82.             cmdHandled = LSingleDoc::ObeyCommand(inCommand, ioParam);
  83.             break;
  84.     }
  85.     return cmdHandled;
  86. }
  87.  
  88. // ---------------------------------------------------------------------------
  89. //    This function enables menu commands.
  90. void
  91. CCounterDocument::FindCommandStatus(
  92.     CommandT    inCommand,
  93.     Boolean        &outEnabled,
  94.     Boolean        &outUsesMark,
  95.     Char16        &outMark,
  96.     Str255        outName)
  97. {
  98.     switch (inCommand) {
  99.         // Return menu item status according to command messages.
  100.  
  101.         case cmd_SetValue:
  102.             outEnabled = true;            // enable the button
  103.             break;
  104.  
  105.         case cmd_Increment:
  106.             outEnabled = true;            // enable the button
  107.             break;
  108.  
  109.         case cmd_Decrement:
  110.             outEnabled = true;            // enable the button
  111.             break;
  112.  
  113.         default:
  114.             LSingleDoc::FindCommandStatus(inCommand, outEnabled,
  115.                                                 outUsesMark, outMark, outName);
  116.             break;
  117.     }
  118. }
  119.  
  120. // ---------------------------------------------------------------------------
  121. LWindow*
  122. CCounterDocument::MakeControlsWindow()
  123. {
  124.     LWindow* theWindow = LWindow::CreateWindow(rWindow_Sample, this );
  125.     mCaption = dynamic_cast<LCaption*>(theWindow->FindPaneByID(kCaption));
  126.     ThrowIfNil_(mCaption);
  127.     theWindow->Show();
  128.     return theWindow;
  129. }
  130.  
  131. // ---------------------------------------------------------------------------
  132. Boolean
  133. CCounterDocument::AskForValue(Int32& newValue)
  134. {
  135.     Str255 numStr;
  136.     Boolean theResult = UModalDialogs::AskForOneNumber(this,
  137.                             rDialog_Value, kEditField, newValue);
  138.     return theResult;
  139. }
  140.  
  141. // ---------------------------------------------------------------------------------
  142. void
  143. CCounterDocument::NameNewDoc()
  144. {
  145.     LStr255    theTitle("\pUntitled");
  146.     // Find the first available title. We could also check the window
  147.     // pane id if we wanted to make sure we didn't collide with other
  148.     // window types.
  149.     Int32    theNumber = 0;
  150.     while (UWindows::FindNamedWindow(theTitle) != nil ) {
  151.         // An existing window has the current name
  152.         // Increment counter and try again.
  153.         ++theNumber;
  154.         theTitle = "\pUntitled ";
  155.         theTitle += static_cast<Int32>(theNumber);
  156.     }            
  157.     // Finally, set window title.
  158.     mWindow->SetDescriptor(theTitle);
  159. }
  160.  
  161. // ---------------------------------------------------------------------------------
  162. void
  163. CCounterDocument::OpenFile(FSSpec& inFileSpec)
  164. {
  165.     mFile = nil;
  166.     // Create a new file object.
  167.     StDeleter<LFile> theFile(new LFile(inFileSpec));
  168.     
  169.     // Open the data fork; get the date; close the data fork
  170.     theFile->OpenDataFork(fsRdWrPerm);    
  171.     Int32** valueHdl = (Int32**)theFile->ReadDataFork();
  172. //    cout << "**valueHdl: " << **valueHdl << endl;
  173.     theFile->CloseDataFork();
  174.     
  175.     // Put the contents in the view
  176.     // and clear the dirty flag.
  177.     stCounter.SetValue((Int32)**valueHdl);
  178.     ::DisposeHandle((Handle)valueHdl);
  179.     mCaption->SetValue(stCounter.GetValue());    
  180.     mIsDirty = false;
  181.     
  182.     // Set the window title to the name of the file and
  183.     // flag that the document has an associated file.
  184.     mWindow->SetDescriptor(inFileSpec.name);
  185.     mIsSpecified = true;
  186.  
  187.     mFile = theFile.Release();
  188. }
  189.  
  190. // ---------------------------------------------------------------------------------
  191. Boolean
  192. CCounterDocument::IsModified()
  193. {
  194.     return mIsDirty;
  195. }
  196.  
  197. // ---------------------------------------------------------------------------------
  198. void
  199. CCounterDocument::DoAESave(FSSpec& inFileSpec, OSType inFileType)
  200. {
  201. //    cout << "inFileType:" << inFileType << endl;
  202.     delete mFile;    // this does nothing to the actual file on disk.
  203.     mFile = nil;
  204.     mFile = new LFile(inFileSpec);
  205.     
  206.     // Get the proper file type.
  207.     OSType    theFileType = kDocType;
  208.     if (inFileType != fileType_Default) {
  209.         theFileType = inFileType;
  210.     }
  211. //    cout << "File type:" << theFileType << endl;
  212. //    cout << "kCreatorType:" << kCreatorType << endl;
  213.     mFile->CreateNewDataFile(kCreatorType, theFileType);
  214.     DoSave();
  215.     mWindow->SetDescriptor(inFileSpec.name);    // set window title
  216.     mIsDirty = false;
  217. }
  218.  
  219. // ---------------------------------------------------------------------------------
  220. void
  221. CCounterDocument::DoSave()
  222. {
  223.     mFile->OpenDataFork(fsRdWrPerm);
  224.     Int32 value = stCounter.GetValue();
  225. //    cout << value << endl;
  226.     mFile->WriteDataFork(&value, 32);        // should use sizeof()
  227.     mFile->CloseDataFork();
  228.     mIsDirty = false;
  229. }
  230.  
  231. // ---------------------------------------------------------------------------------
  232. void
  233. CCounterDocument::DoRevert()
  234. {
  235.     mFile->OpenDataFork(fsRdWrPerm);
  236. /*    Handle    theTextH = mFile->ReadDataFork();
  237.     mFile->CloseDataFork();
  238.     mTextView->SetTextHandle( theTextH );
  239.     mTextView->SetDirty( false );
  240.     ::DisposeHandle( theTextH );
  241.     mTextView->Refresh();
  242. */
  243. }
  244.  
  245. // ---------------------------------------------------------------------------------
  246. void
  247. CCounterDocument::DoPrint()
  248. {
  249. /*    // Create the printout.
  250.     StDeleter<LPrintout> thePrintout(LPrintout::CreatePrintout( rPPob_TextPrintout ));
  251.     ThrowIfNil_(thePrintout.Get());
  252.     
  253.     // Set the print record.
  254.     if (mPrintRecordH) {
  255.         thePrintout->SetPrintRecord( mPrintRecordH );
  256.     }
  257.     
  258.     // Get the text placeholder.
  259.     LPlaceHolder    *thePlaceholder;
  260.     thePlaceholder = dynamic_cast<LPlaceHolder*>(thePrintout->FindPaneByID( kTextPlaceholder ));
  261.     ThrowIfNil_(thePlaceholder);
  262.     
  263.     // Install the text view in the placeholder.
  264.     thePlaceholder->InstallOccupant( mTextView, atNone );
  265.     
  266.     // Set the frame size.
  267.     SetPrintFrameSize();
  268.     
  269.     // Print.
  270.     thePrintout->DoPrintJob();
  271.     
  272.     // Delete the printout (handled automatically by the
  273.     // StDeleter object). The text view is returned
  274.     // to the window when the placeholder is destroyed.
  275. */
  276. }
  277.  
  278. // ---------------------------------------------------------------------------------
  279. void
  280. CCounterDocument::SetPrintFrameSize()
  281. {
  282.     /*
  283.     // Get the frame size.
  284.     SDimension16    theFrameSize;
  285.     mTextView->GetFrameSize( theFrameSize );
  286.     
  287.     // Get the text edit record handle.
  288.     TEHandle    theTextEditH = mTextView->GetMacTEH();
  289.     
  290.     // Calculate the number of lines per page.
  291.     Int16    theLinesPerPage;
  292.     theLinesPerPage = theFrameSize.height / (**theTextEditH).lineHeight;
  293.  
  294.     // Resize the frame to an integral number of lines.
  295.     mTextView->ResizeFrameTo( theFrameSize.width,
  296.         (**theTextEditH).lineHeight * theLinesPerPage, false );
  297. */
  298. }
  299.